/**
 * @(#)OrderCartServiceImpl.java, 2018/2/12.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.service.basic.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wangzx.exercise.enums.BuyStateType;
import org.wangzx.exercise.meta.bean.ItemBean;
import org.wangzx.exercise.meta.bean.OrderCartBean;
import org.wangzx.exercise.meta.convertor.OrderCartConvertor;
import org.wangzx.exercise.meta.vo.AccountVO;
import org.wangzx.exercise.meta.vo.CartVO;
import org.wangzx.exercise.meta.vo.ItemVO;
import org.wangzx.exercise.service.basic.OrderCartService;
import org.wangzx.exercise.service.core.ItemCoreService;
import org.wangzx.exercise.service.core.OrderCartCoreService;
import org.wangzx.exercise.service.core.UserCoreService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Service("orderCartService")
public class OrderCartServiceImpl implements OrderCartService {
    static private final Logger logger = LoggerFactory.getLogger(OrderCartServiceImpl.class);

    @Autowired
    private OrderCartCoreService orderCartCoreService;

    @Autowired
    private ItemCoreService itemCoreService;

    @Autowired
    private UserCoreService userCoreService;

    @Override
    public boolean insert(ItemVO itemVO, long buyerId) {
        OrderCartBean bean = OrderCartConvertor.voToBo(itemVO);
        bean.setBuyer(buyerId);
        bean.setState(BuyStateType.CART);
        return orderCartCoreService.insert(bean) > 0;
    }

    @Override
    public List<CartVO> getCart(long buyerId) {
        List<CartVO> result = new ArrayList<>();
        List<OrderCartBean> orderCartBeans = orderCartCoreService.getByBuyerAndState(buyerId, BuyStateType.CART);
        for(OrderCartBean bean : orderCartBeans) {
            CartVO cartVO = new CartVO();

            long elemId = bean.getItemId();
            cartVO.setId(elemId);

            ItemBean itemBean = itemCoreService.getItemById(elemId);
            cartVO.setName(itemBean.getName());

            cartVO.setCount(bean.getCount());
            cartVO.setPrice(itemBean.getPrice());

            result.add(cartVO);
        }
        return result;
    }

    @Override
    @Transactional
    public boolean addOrder(List<CartVO> list, long buyerId) {
        boolean result = true;

        for(CartVO vo : list) {
            OrderCartBean bean = orderCartCoreService.getByBuyerAndItemId(buyerId, vo.getId());
            if(bean == null) {
                return false;
            }
            bean.setCount(vo.getCount());
            bean.setPrice(vo.getPrice());
            bean.setState(BuyStateType.BUY);
            bean.setCreateTime(System.currentTimeMillis());
            bean.setUpdateTime(System.currentTimeMillis());
            result = result && orderCartCoreService.updateOrderCart(bean);

            result = result && itemCoreService.addSellCount(vo.getId(), vo.getCount());
        }
        return result;
    }

    @Override
    public List<AccountVO> getAccountList(long buyerId) {
        List<AccountVO> result = new ArrayList<>();
        List<OrderCartBean> beanList = orderCartCoreService.getByBuyerAndState(buyerId, BuyStateType.BUY);

        for(OrderCartBean bean : beanList) {
            AccountVO vo = new AccountVO();

            ItemBean itemBean = itemCoreService.getItemById(bean.getItemId());
            vo.setId(itemBean.getId());
            vo.setName(itemBean.getName());
            vo.setPicAddr(itemBean.getPicAddr());
            vo.setCount(bean.getCount());
            vo.setPrice(bean.getPrice());
            vo.setTime(bean.getCreateTime());

            result.add(vo);
        }

        return result;

    }
}
